package cardGame;

import java.util.List;
import java.util.Scanner;

public class ActualWarGame extends Game{

		@Override
		protected int getMaxPlayers() {
			return 25;
		}

		@Override
		protected int getMinPlayers() {
			return 2;
		}
		
		@Override
		protected void play(int numPlayers) {
			if (numPlayers <= getMaxPlayers() && numPlayers >= getMinPlayers()){
			initPlayers(numPlayers);
			deal(new HoyleDeck(), numPlayers);
//			for (Player p : this.players){
//				System.out.println(p.getHand().size());
//			}
			playRound(numPlayers);
			}
			else {
				System.out.println("Oops. Not the right number of players.");
			}
		}

		private void initPlayers(int numPlayers) {
			for (int i = 0; i < numPlayers; i++) {
				this.players.add(new Player("Player" + (i + 1)));
			}
		}

		private void deal(HoyleDeck deck, int numPlayers) {
			deck.shuffle();
			int numDealt = 0;
			Card card = deck.deal(numPlayers);
			while (card != null) {
				this.players.get(numDealt++ % numPlayers).getHand().add(card);
				card = deck.deal(numPlayers);
			}
		}

		private void playRound(int numPlayers) {
			
			Card[] pile = new Card[numPlayers];
			for (int i = 0; i < numPlayers; ){
			for (Player p : this.players) {
				String topCrd = "";
					pile[i] = p.getHand().get(0);
					topCrd += pile[i].getFaceValue().toString();
					topCrd += " of ";
					topCrd += pile[i].getSuit().toString();
					topCrd += "S ";
				System.out.println(p.getName() + ": " + topCrd);
				p.getHand().remove(0);
				System.out.println(p.getHand().size());
				i++;
			}
			for (Player p : this.players) {
			if (p.getName() == "Player" + winningPlayer(numPlayers, pile)){
				for (int i2 = 0; i2 < pile.length; i2++)
				p.getHand().add(pile[i2]);
				System.out.println("Yay!");
				
			}
			}
			System.out.println(winningPlayer(numPlayers, pile));
			System.out.println(pile[0].getFaceValue() + " of " + pile[0].getSuit() + "SSSSS, " + pile[1].getFaceValue() + " of " + pile[1].getSuit() + "SSSSS");
			}
		
			
		}

		public int winningPlayer(int numPlayers, Card[] pile ){
//			for (int i = 1; i < (numPlayers - 1); i++) {
//				if (pile[i].getFaceValue().ordinal() + 1 <= pile[i-i].getFaceValue().ordinal() + 1) {
//					System.out.println(i);
//					return false;
//				}
//				
//			}
//			
//			return true;
			int pos = 0;
			int posOfL = 0;
			int SIZE = (numPlayers - 1);
			int largest = pile[posOfL].getFaceValue().ordinal() + 1;
			System.out.println(posOfL + " " + largest + " " + pile[pos].getFaceValue());
			
			while (pos < SIZE)
			{
			if ((pile[pos].getFaceValue().ordinal() + 1) > largest);
			{
			posOfL = pos;
			}
			pos++;
			}
			
			return (posOfL + 1);

		}
		
//		private int countHand(List<Card> hand) {
//			int sum = 0;
//			for (Card card : hand) {
//				// if (card.getFaceValue().ordinal() + 1 <= 10){
//				sum += card.getFaceValue().ordinal() + 1;
//				// }
//				// else {
//				// sum += 10;
//				// }
//			}
//			return sum;
//		}
		

		public static void main(String[] args) {
			System.out.println("Enter the number of players (2 to 25): ");
			Scanner sc = new Scanner(System.in);
			int numPlayers = sc.nextInt();
			new ActualWarGame().play(numPlayers);
		}


	}
